package com.carkeeper.subject.controller;

import com.alibaba.excel.EasyExcel;
import com.carkeeper.common.utils.*;
import com.carkeeper.subject.pojo.Merchant;
import com.carkeeper.subject.pojo.Store;
import com.carkeeper.subject.pojo.StorePoi;
import com.carkeeper.subject.service.StoreService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@RestController
@RequestMapping("/store")
@Api(tags = "服务商接口api")//标签专门用来标注类的
public class StoreController {
    @Autowired
    private StoreService storeService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /*
    * 根据id查询store
    * */
    @GetMapping("/findById/{id}")
    @ApiOperation("id查询服务商接口")//标注方法
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "id",value = "主键")
    })
    public Result<Store> findById(@PathVariable("id") Long id){
        try {
            Store store = storeService.findById(id);
            return new Result<Store>(true, StatusCode.OK,"查询成功",store);
        }catch (Exception e){
            e.printStackTrace();
            return new Result<Store>(false, StatusCode.ERROR,"查询失败");
        }
    }

    /*
    * 多条件查询
    * */
    @GetMapping("/findBySearch")
    @ApiOperation("多条件查询接口")//标注方法
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "simpleName",value = "汽修厂简称"),
            @ApiImplicitParam(name = "code",value = "汽修厂编码"),
            @ApiImplicitParam(name = "province",value = "省"),
            @ApiImplicitParam(name = "city",value = "市"),
            @ApiImplicitParam(name = "county",value = "区县"),
            @ApiImplicitParam(name = "contactPhone",value = "联系电话"),
            @ApiImplicitParam(name = "isChain",value = "是否连锁"),
            @ApiImplicitParam(name = "status",value = "锁定状态")
    })
    public Result<List<Store>> findBySearch(Store store){
        try {
            List<Store> storeSearch = storeService.findBySearch(store);
            return new Result<List<Store>>(true, StatusCode.OK,"查询成功",storeSearch);
        }catch (Exception e){
            e.printStackTrace();
            return new Result<List<Store>>(false, StatusCode.ERROR,"查询失败");
        }

    }

    /*
    * 分页查询
    * */
    @GetMapping("/findPage/{page}/{size}")
    @ApiOperation("分页查询接口")//标注方法
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "page",value = "页"),
            @ApiImplicitParam(name = "size",value = "条数")
    })
    public Result findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        if (page == null){
            page = 1;
        }
        if (size == null){
            size = 15;
        }
        try {
            Page<Store> page1 = storeService.findPage(page, size);
            PageResult pageResult = new PageResult(page1.getTotal(),page1.getResult());
            return new Result(true,StatusCode.OK,"查询成功",pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }


    /*
    * 文件上传
    * */
    @RequestMapping("/upload")
    @ApiOperation("图片七牛云上传接口")//标注方法
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "imgFile",value = "图片名称"),
    })
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        //获取原始文件名称
        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(index - 1); // .jpg 获取后缀
        //文件新名称
        String fileName = UUID.randomUUID().toString().replace("-","")+substring;
        try {
            //将文件上传到七牛云服务器
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //将上传的文件存存redis中   set存储
            stringRedisTemplate.boundSetOps(RedisConstant.PIC_RESOURCES).add(fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"上传失败");
        }
        return new Result(true,StatusCode.OK,"上传成功",fileName);
    }


    /*
    * 添加服务商
    * */
    @PostMapping("/add")
    @ApiOperation("新增接口")//标注方法
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "merchant",value = "条件实体类")
    })
    public Result add(@RequestBody Merchant merchant){
        try {
            storeService.add(merchant);
            return new Result(true, StatusCode.OK,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }


    /*
     * 锁定服务商
     * */
    @PutMapping("/lock/{id}")
    @ApiOperation("锁定接口")//标注方法
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "id",value = "主键")
    })
    public Result lock(@PathVariable("id") Long id){
        try {
            storeService.lock(id);
            return new Result(true,StatusCode.OK,"锁定成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"锁定失败");
        }
    }


    /*
     * 解锁服务商
     * */
    @PutMapping("/unlock/{id}")
    @ApiOperation("解锁接口")//标注方法
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "id",value = "主键")
    })
    public Result unlock(@PathVariable("id") Long id){
        try {
            storeService.unlock(id);
            return new Result(true,StatusCode.OK,"解锁成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"解锁失败");
        }
    }

    /*
    * 根据id查询查询商家 和商家详情
    * */
    @GetMapping("/findByStoreId/{id}")
    @ApiOperation("id查询服务商+商家详情接口")//标注方法
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "id",value = "主键")
    })
    public Result<Merchant> findByStoreId(@PathVariable("id") Long id){
        try {
            Merchant merchant = storeService.findByStoreId(id);
            return new Result<Merchant>(true,StatusCode.OK,"查询成功",merchant);
        }catch (Exception e){
            e.printStackTrace();
            return new Result<Merchant>(false,StatusCode.ERROR,"查询失败");
        }
    }

    /*
    * 修改服务商主体
    * */
    @PutMapping("/update")
    @ApiOperation("修改服务商+商家详情接口")//标注方法
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "merchant",value = "实体类集合")
    })
    public Result update(@RequestBody Merchant merchant){
        try {
            storeService.update(merchant);
            return new Result(true,StatusCode.OK,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
    }

    /*
     * 根据tb_order表的storeID查询store表
     * */

    @GetMapping("/findByOrderStoreId/{id}")
    @ApiOperation("id查询服务商接口")//标注方法
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "id",value = "主键")
    })
    public Result<Store> dByOrderStoreId(@PathVariable("id") Long id){
        try {
            Store store = storeService.findByOrderStoreId(id);
            return new Result<Store>(true,StatusCode.OK,"查询成功",store);
        }catch (Exception e){
            e.printStackTrace();
            return new Result<Store>(false,StatusCode.ERROR,"查询失败");
        }
    }

    /*
     * 文件下载
     * */
    @RequestMapping("/download")
    @ApiOperation("POI报表导出接口")//标注方法
    public void download(HttpServletResponse response) throws Exception{
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName= URLEncoder.encode("服务商","UTF-8");
        response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");
        List<StorePoi> list = storeService.findAll();
        EasyExcel.write(response.getOutputStream(), StorePoi.class).sheet().doWrite(list);
    }
}

